package com.xfusion.chatglm.data.trigger.job;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.CloseOrderRequest;
import com.xfusion.chatglm.data.domain.order.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description 超时关单任务
 */
@Slf4j
@Component()
public class TimeoutCloseOrderJob {

    @Resource
    private IOrderService orderService;
    @Resource
    private AlipayClient alipayClient;

    @Autowired(required = false)
    private NativePayService payService;
//    @Value("${wxpay.config.mchid}")
//    private String mchid;
    @Value("${alipay.app_id}")
    private String appid;
    @Value("${alipay.merchant_private_key}")
    private String privateKey;
    @Value("${alipay.gatewayUrl}")
    private String gatewayUrl;
    @Value("${alipay.alipay_public_key}")
    private String publicKey;

    @Scheduled(cron = "0 0/10 * * * ?")
    public void exec() {
        try {
            if (null == payService) {
                log.info("定时任务，订单支付状态更新。应用未配置支付渠道，任务不执行。");
                return;
            }
            List<String> orderIds = orderService.queryTimeoutCloseOrderList();
            if (orderIds.isEmpty()) {
                log.info("定时任务，超时30分钟订单关闭，暂无超时未支付订单 orderIds is null");
                return;
            }
            for (String orderId : orderIds) {
                boolean status = orderService.changeOrderClose(orderId);
                //微信关单；暂时不需要主动关闭
//                CloseOrderRequest request = new CloseOrderRequest();
                //request.setMchid(mchid);

//                request.setOutTradeNo(orderId);
//                payService.closeOrder(request);

                //支付宝关单
                AlipayClient alipayClient =
                        new DefaultAlipayClient(gatewayUrl,
                                appid,
                                privateKey,
                                "json",
                                "GBK",
                                publicKey);
                AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
                request.setBizContent("{" +
                        "    \"out_trade_no\":\""+orderId+"\"" +
                        "  }");
                AlipayTradeCloseResponse response = alipayClient.execute(request);
                if (response.isSuccess()){
                    log.info("订单号："+orderId+"支付宝关闭订单成功并删除支付二维码");
                }else{
                    log.info("订单号："+orderId+"支付宝关闭订单失败"+response.getSubCode()+response.getSubMsg());
                }
                log.info("定时任务，超时30分钟订单关闭 orderId: {} status：{}", orderId, status);
            }
        } catch (Exception e) {
            log.error("定时任务，超时15分钟订单关闭失败", e);
        }
    }

}
